package view.admin.userManage;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXCheckBox;
import com.jfoenix.controls.JFXTextField;
import dao.UserDao;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.util.Callback;
import pojo.user.UserEntity;
import pojo.user.UserProperty;
import view.Main;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Limohan
 */
public class UserManageController {
    private Main myApp;
    private static List<UserEntity>userEntities = new ArrayList<>() ;
    public UserManageController() {
    }
    public void setApp(Main myApp){
        this.myApp = myApp;
    }
    public String getId() throws IOException {
        return userEntities.get(0).getId();
    }


    @FXML
    private JFXButton newUser;

    @FXML
    private JFXButton searchNewUser;

    @FXML
    private JFXTextField searchName;

    @FXML
    private TableView<UserProperty> userTable;

    @FXML
    private TableColumn<UserProperty,Void> option;

    @FXML
    private TableColumn<UserProperty,String> id;

    @FXML
    private TableColumn<UserProperty,String> account;

    @FXML
    private TableColumn<UserProperty,String> name;

    @FXML
    private TableColumn<UserProperty,String> phone;

    @FXML
    private TableColumn<UserProperty,String> power;

    @FXML
    private JFXButton deleteUser;

    @FXML
    private JFXButton changeUserInfo;

    @FXML
    void initialize() throws IOException {
        UserDao userDao = new UserDao();
        this.displayInfo(userDao.findUserList());
    }

    @FXML
    void setChangeUser(ActionEvent event) throws Exception {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        if(userEntities.size()>1) {
            alert.setHeaderText("!");
            alert.setContentText("只能选择一个哦");
            alert.showAndWait();
            userEntities = new ArrayList<>();
            this.initialize();
        }else if(userEntities.size() == 0){
            alert.setHeaderText("!");
            alert.setContentText("至少选择一个");
            alert.showAndWait();
            userEntities = new ArrayList<>();
            this.initialize();

        }
        else {
            myApp.goToSetChangeUserPage();
            userEntities = new ArrayList<>();
            this.initialize();
        }
        userEntities=new ArrayList<>();
    }

    @FXML
    void setDeleteUser(ActionEvent event) throws Exception {
        userTable.setItems(FXCollections.emptyObservableList());
        UserDao userDao  = new UserDao();
        for(UserEntity user:userEntities)
        userDao.deleteUser(user.getId());
        this.initialize();
        userEntities=new ArrayList<>();
    }

    @FXML
    void setNewUser(ActionEvent event) throws Exception {
        myApp.goToSetNewUserPage();
        this.initialize();
        userEntities=new ArrayList<>();

    }

    @FXML
    void setSearchUser(ActionEvent event) throws IOException {
        UserDao userDao = new UserDao();
        String str =  searchName.getText();
        List<UserEntity> users = userDao.findUserByName(str);
        this.displayInfo(users);
        userEntities=new ArrayList<>();

    }

    private void addButtonToTable(){
        UserDao userDao = new UserDao();
        Callback<TableColumn<UserProperty, Void>, TableCell<UserProperty, Void>> cellFactory = new Callback<TableColumn<UserProperty, Void>, TableCell<UserProperty, Void>>() {
            @Override
            public TableCell<UserProperty, Void> call(final TableColumn<UserProperty, Void> param) {
                final TableCell<UserProperty, Void> cell = new TableCell<>() {

                    final JFXCheckBox btn = new JFXCheckBox();

                    {
                        btn.setOnAction((ActionEvent event) -> {
                            UserProperty userProperty = getTableView().getItems().get(getIndex());
                            try {
                                userEntities.add(userDao.findUser(userProperty.getAccount()));
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                        });
                    }

                    @Override
                    public void updateItem(Void item, boolean empty) {
                        super.updateItem(item, empty);
                        if (empty) {
                            setGraphic(null);
                        } else {
                            setGraphic(btn);
                        }
                    }
                };
                return cell;
            }
        };

        option.setCellFactory(cellFactory);

    }

    void displayInfo(List<UserEntity> userEntityList){
        userTable.getItems();
        ObservableList<UserProperty> users = FXCollections.observableArrayList();
        if(userEntityList!=null){
            for(UserEntity user:userEntityList){

                UserProperty userProperty = new UserProperty();
                userProperty.setAccount(user.getAccount());
                userProperty.setId(user.getId());
                userProperty.setName(user.getName());
                userProperty.setPhone(user.getPhone());
                userProperty.setPower(user.getPower());
                users.add(userProperty);
            }
            addButtonToTable();
            id.setCellValueFactory(param -> param.getValue().idProperty());
            account.setCellValueFactory(param -> param.getValue().accountProperty());
            name.setCellValueFactory(param -> param.getValue().nameProperty());
            phone.setCellValueFactory(param -> param.getValue().phoneProperty());
            power.setCellValueFactory(param -> param.getValue().powerProperty());

            userTable.setItems(users);
        }else{
            userTable.setItems(FXCollections.emptyObservableList());
        }

    }
    @FXML
    void setReturnTable(ActionEvent event) throws IOException {
        UserDao userDao = new UserDao();
        this.displayInfo(userDao.findUserList());

    }
    @FXML
    void returnMenu(ActionEvent event) throws Exception {
        myApp.goToAdminLoginPage();

    }




}
